<template >
    <div>
        <div class="con-box">
            <div class="cons">
                <!-- 表单验证 -->
                <div class="img-box"> <img src="../assets/logo.png" alt=""></div>
                <div class="inp-box">
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
                        <!-- 用户名 -->
                        <el-form-item prop="username">
                            <el-input v-model="ruleForm.username" prefix-icon="el-icon-user-solid"></el-input>
                        </el-form-item>
                        <!-- 密码 -->
                        <el-form-item prop="password">
                            <el-input type="password" v-model="ruleForm.password" prefix-icon="el-icon-lock"></el-input>
                        </el-form-item>
                        <div class="btn">
                            <el-form-item>
                                <el-button type="primary" @click="tohome('ruleForm')">登录</el-button>
                                <el-button @click="resetForm('ruleForm')">重置</el-button>
                            </el-form-item>
                        </div>
                    </el-form>
                </div>

            </div>

        </div>
</div>
</template>
<script>
import { getLogin, } from "../api/api"
export default {
    data() {
        return {
            ruleForm: {
                username: "",
                password: ""
            },
            rules: {
                username: { required: true, message: '请输用户名', trigger: 'blur' },
                password: { required: true, message: '请输密码', trigger: 'blur' },
            }
        }
    },
    methods: {
        tohome(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    // console.log(111);
                    // console.log('submit!');
                    getLogin(this.ruleForm).then(res => {
                        console.log(res);
                        if (res.meta.status == 200) {
                            this.$message({
                                message: '登录成功',
                                type: 'success'
                            });
                            this.$router.push("/welcome")
                            localStorage.setItem("token", res.data.token)

                        } else {
                            this.$message({
                                message: '登录失败',
                                type: 'error'
                            });
                        }
                    })


                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        }
    },
    created() {

    },
}
</script>
<style lang="scss" scoped>
.con-box {
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

.cons {
    width: 450px;
    height: 300px;
    background-color: #fff;
    padding: 1px 15px;
    box-sizing: border-box;
    box-shadow: 0 0 10px #ddd;

    position: relative;


}

.img-box {
    width: 129px;
    height: 129px;
    background-color: #eeeeee;
    border-radius: 50%;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);

    box-shadow: 0 0 10px #ddd;
    border: 11px solid #fff;



    img {
        width: 129px;
        height: 129px;
        border-radius: 50%;
    }
}

.inp-box {
    margin-top: 120px;
}

.btn {
    text-align: right;
}
</style>